import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '../store/user'

const routes = [
    {
        path: '/',
        name: 'Home',
        alias: '/questions',
        component: () => import('../views/Home.vue'),
        meta: {
            title: '首页 - 爱编程',
        },
    },
    {
        path: '/questions/ask',
        name: 'AskQuestions',
        component: () => import('../views/Publish.vue'),
        meta: {
            requiresAuth: true,
            title: '提问 - 爱编程',
        },
    },
    {
        path: '/questions/:id(\\d+)',
        name: 'Detail',
        component: () => import('../views/Detail.vue'),
    },
    {
        path: '/users/:id',
        name: 'User',
        component: () => import('../views/Profile.vue'),
        meta: {
            requiresAuth: true,
        },
    },
    {
        path: '/questions/:id/edit',
        name: 'EditQuestion',
        component: () => import('../views/Edit.vue'),
        meta: {
            requiresAuth: true,
            title: '编辑 - 爱编程',
        },
    },
    {
        path: '/users/:id/edit',
        name: 'EditProfile',
        component: () => import('../views/Setting.vue'),
        meta: {
            requiresAuth: true,
            title: '设置 - 爱编程',
        },
    },
    {
        path: '/search/:keyword',
        name: 'Search',
        component: () => import('../views/Search.vue'),
        meta: {
            title: '搜索结果 - 爱编程',
        },
    },
    {
        path: '/tags',
        name: 'Tag',
        component: () => import('../views/TagList.vue'),
        meta: {
            title: '标签 - 爱编程',
        },
    },
    {
        path: '/rankings',
        name: 'Ranking',
        component: () => import('../views/Ranking.vue'),
        meta: {
            title: '排行榜 - 爱编程',
        },
    },
    {
        path: '/user-list',
        name: 'UserManagement',
        component: () => import('../views/UserList.vue'),
        meta: {
            requiresAuth: true,
            title: '用户管理 - 爱编程',
        },
    },
    {
        path: '/question-list',
        name: 'QuestionManagement',
        component: () => import('../views/QuestionList.vue'),
        meta: {
            requiresAuth: true,
            title: '问题管理 - 爱编程',
        },
    },
    {
        path: '/404',
        name: 'NOTFound',
        meta: {
            title: 'Page not found',
            requiresAuth: false,
        },
        component: () => import('../views/NotFound.vue'),
    },
    {
        path: '/:pathMatch(.*)*',
        redirect: '/404',
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes,
})

router.beforeEach((to, from) => {
    const userStore = useUserStore()
    if (to.meta.title) {
        document.title = to.meta.title
    }
    if (to.meta.requiresAuth && userStore.token === '') {
        router.push('/').then(r => {
            window.$message?.warning('你没有权限，请登录后再试！')
        })
        return false
    } else {
        return true
    }
})

export default router
